<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon style="fontSize:12px" icon="ant-design:down-outlined"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <ArticleGroupDrawer @register="registerDrawer" @success="reload" />
  </div>
</template>
<script lang="ts" name="ArticleGroup" setup>
import { ref } from 'vue';
import { BasicTable, TableAction } from '/@/components/Table';
import { useDrawer } from '/@/components/Drawer';
import ArticleGroupDrawer from './ArticleGroupDrawer.vue';
import { useMethods } from '/@/hooks/system/useMethods';
import { useGlobSetting } from '/@/hooks/setting';
import { Icon } from '/@/components/Icon'
import { columns, searchFormSchema } from './articleGroup.data';
import { listGroup, deleteGroup, batchDeleteGroup } from '/@/api/sys/article';
import { useListPage } from '/@/hooks/system/useListPage';
const glob = useGlobSetting();
const [registerDrawer, { openDrawer }] = useDrawer();
const { prefixCls, tableContext } = useListPage({
  designScope: 'article-group',
  tableProps: {
    title: '文章分组',
    api: listGroup,
    columns: columns,
    bordered: false,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
    formConfig: {
      schemas: searchFormSchema,
    },
  }
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

function handleAdd() {
  openDrawer(true, {
    isUpdate: false,
  });
}

function handleEdit(record) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

async function handleDelete(record) {
  await deleteGroup({ id: record.id }, reload);
}

async function batchHandleDelete() {
  await batchDeleteGroup({ ids: selectedRowKeys.value }, reload);
}

function getActions(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      // ifShow: record.sendStatus == 0
    },
    {
      label: '删除',
      color: 'error',
      ifShow: record.sendStatus != 1,
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      },
    },
  ]
}

</script>
